﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UIObjects
{
    public class LogEntryData
    {
        /// <summary>
        /// Gets or sets the log level of the log entry.
        /// This is the intended level that the log entry should be written.
        /// This may cause the log entry to be ignored if the current
        /// <see cref="ILogStrategy"/> has a higher threshold log level.
        /// </summary>
        /// <value>The log level.</value>
        public LogLevel LogLevel { get; set; }

        /// <summary>
        /// Gets or sets the message to be logged.
        /// </summary>
        /// <value>The message.</value>
        public string Message { get; set; }

        /// <summary>
        /// Gets or sets the code location of the call to log.
        /// </summary>
        /// <value>The code location.</value>
        public CodeLocation CodeLocation { get; set; }

        /// <summary>
        /// Gets or sets the exception memento, representing
        /// an exception that is to be logged.
        /// May be null.
        /// </summary>
        /// <value>The exception memento.</value>
        public ExceptionMemento ExceptionMemento { get; set; }

        /// <summary>
        /// Gets or sets the name of the thread that requested
        /// the log entry to take place.
        /// </summary>
        /// <value>The name of the thread.</value>
        public string ThreadName { get; set; }

        /// <summary>
        /// Gets or sets the managed thread id that requested
        /// the log entry to take place.
        /// </summary>
        /// <value>The managed thread id.</value>
        public int ManagedThreadId { get; set; }

        /// <summary>
        /// Gets or sets the time that the log request was made.
        /// </summary>
        /// <value>The time that the log request was made.</value>
        public DateTime OccuredAt { get; set; }


        /// <summary>
        /// Gets or sets additional custom properties.
        /// </summary>
        /// <value>The custom properties.</value>
        public IDictionary<string, object> Properties { get; set; }

        public override string ToString()
        {
            var propertiesBuilder = new StringBuilder();
            if (Properties != null)
            {
                foreach (var pair in Properties)
                {
                    propertiesBuilder.Append(pair.Key);
                    propertiesBuilder.Append('=');
                    propertiesBuilder.Append(pair.Value);
                    propertiesBuilder.Append(";");
                }
            }

            string result = string.Format("{0} , Message:{1}, LogLevel: {2}, ThreadName: {3}, ManagedThreadId: {4}, Properties: {5}, {6}",
                    CodeLocation, Message, LogLevel, ThreadName, ManagedThreadId, propertiesBuilder, base.ToString());
            return result;
        }
    }
}
